package com.ccb.sc.member.controller;

import com.ccb.sc.auth.TokenAuth;
import com.ccb.sc.auth.TokenModel;
import com.ccb.sc.member.service.HeadsApplyService;
import com.ccb.sc.common.bind.annotation.CurrentUser;
import com.ccb.sc.common.bind.response.ResBody;
import com.ccb.sc.common.modules.shopkeeper.entity.Shopkeeper;
import com.ccb.sc.common.modules.shopkeeper.enums.ApplyStatus;
import com.ccb.sc.common.modules.shopkeeper.service.ShopkeeperAuthService;
import com.ccb.sc.common.modules.shopkeeper.service.ShopkeeperService;
import com.ccb.sc.common.modules.shopkeeper.vo.ShopkeeperApplyCheckVo;
import com.ccb.sc.common.modules.shopkeeper.vo.ShopkeeperAuthCreateVo;
import com.ccb.sc.common.modules.shopkeeper.vo.ShopkeeperAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 店主 前端控制器
 * </p>
 *
 * @author ken
 * @since 2023-02-12
 */
@TokenAuth
@RestController
@RequestMapping("/app/member")
@Api(value = "申请认证管理", tags = "申请认证管理")
public class AuthController {

    private ShopkeeperService shopkeeperService;

    private HeadsApplyService applyService;

    private ShopkeeperAuthService authService;

    @PostMapping("/apply")
    @ApiOperation(value ="店主申请")
    public ResBody<?> apply(@ApiIgnore @CurrentUser TokenModel token) {
        this.applyService.apply(token.getId());
        return ResBody.success();
    }

    @PostMapping("/apply/check")
    @ApiOperation(value ="店主申请校验", response = ShopkeeperApplyCheckVo.class)
    public ResBody<?> checkApply(@ApiIgnore @CurrentUser TokenModel token) {
        ShopkeeperApplyCheckVo checkVo = this.applyService.check(token.getId());
        return ResBody.success(checkVo);
    }


    @GetMapping("/apply")
    @ApiOperation(value ="店主申请状态查询")
    public ResBody<?> queryApply(@ApiIgnore @CurrentUser TokenModel token) {
        ApplyStatus applyStatus = this.applyService.queryApply(token.getId());
        return ResBody.success(applyStatus);
    }

    /*@DeleteMapping("/auth")
    @ApiOperation(value ="删除实名认证")
    public ResBody<?> deleteAuth() {
        this.authService.remove(Wrappers.<ShopkeeperAuth>lambdaQuery().eq(ShopkeeperAuth::getShopkeeperId,StpUtil.getLoginIdAsLong()));
        return ResBody.success();
    }*/

    @GetMapping("/auth")
    @ApiOperation(value ="获取实名认证信息", response = ShopkeeperAuthVo.class)
    public ResBody<?> auth(@ApiIgnore @CurrentUser TokenModel token) {
        Shopkeeper shopkeeper = this.shopkeeperService.findByMemberId(token.getId());
        if (shopkeeper == null) {
            shopkeeper = this.shopkeeperService.create(token.getId());
        }
        ShopkeeperAuthVo shopkeeperAuthVo = this.authService.info(shopkeeper.getId());
        return ResBody.success(shopkeeperAuthVo);
    }

    @PostMapping("/auth")
    @ApiOperation(value ="实名认证", notes = "数据为空或者状态不为审核拒绝时才可以申请认证")
    public ResBody<?> auth(@Validated @RequestBody ShopkeeperAuthCreateVo createVo, @ApiIgnore @CurrentUser TokenModel token) {
        Shopkeeper shopkeeper = this.shopkeeperService.findByMemberId(token.getId());
        this.authService.create(shopkeeper.getId(), createVo);
        return ResBody.success();
    }

    @PostMapping("/alipay-account")
    @ApiOperation(value ="修改支付宝账号")
    public ResBody<?> alipayAccount(String alipayAccount, @ApiIgnore @CurrentUser TokenModel token) {
        Shopkeeper shopkeeper = this.shopkeeperService.findByMemberId(token.getId());
        this.authService.alipayAccount(shopkeeper.getId(), alipayAccount);
        return ResBody.success();
    }

    @Autowired
    public void setShopkeeperService(ShopkeeperService shopkeeperService) {
        this.shopkeeperService = shopkeeperService;
    }

    @Autowired
    public void setApplyService(HeadsApplyService applyService) {
        this.applyService = applyService;
    }

    @Autowired
    public void setAuthService(ShopkeeperAuthService authService) {
        this.authService = authService;
    }

}

